import { defineStore } from 'pinia'
import { ref } from 'vue'
import router from '@/router/index.js'
import { getUserInfo } from '@/api/user.js'
export const setUserStore = defineStore(
  'userInfo',
  () => {
    const token = ref('')
    const userInfo = ref()
    const setToken = (value) => (token.value = value)
    const getsUserInfo = async () => {
      const res = await getUserInfo()
      userInfo.value = res.data.data
    }
    const removeToken = () => {
      token.value = ''
      userInfo.value = {}
      router.push('/login')
    }
    return { token, setToken, removeToken, userInfo, getsUserInfo }
  },
  {
    persist: {
      key: 'userInfo'
    }
  }
)
